/*!
 * 微知通知模块构造函数
 * @author Sherlock
 */
define( [ 'jquery' ], function( $ ) {

	// 声明BigweNotice构造函数
	var BigweNotice = function( options ) {
		this.$body = $( window.top.document.body );
		this.$window = $( window );
		this.init( options );
	}

	BigweNotice.DEFAULTS = {
		customClass: '',
		delay: 4e3,
		template: '<div class="system_notice"><span class="close">&times;</span><p class="gray"><a target="_blank" href="/notice" class="text"></a></p></div>',
		content: '',
		event: {
			mouseenter: function() {
				clearTimeout( $( this ).data( 'timeoutID' ) );
			},
			mouseleave: function() {
				$( this ).data( 'timeoutID', setTimeout( $.proxy( function() {
					BigweNotice.prototype.remove( $( this ) );
				}, this ), 1.5e3 ) );
			}
		}
	}

	/** 初始化参数 */
	BigweNotice.prototype.init = function( options ) {
		this.option = $.extend( {}, BigweNotice.DEFAULTS, options );
		this.$notice = $( this.option.template );
		return this;
	}
	/** 显示通知消息 */
	BigweNotice.prototype.show = function() {
		$( '.text', this.$notice ).text( this.option.body );
		if ( this.option.body != null && this.option.body != '' ) {
			this.$notice.css( 'right', this.$window.width() / 2 - 1200 / 2 );
			this.$notice.appendTo( this.$body ).on( this.option.event ).data( 'timeoutID', setTimeout( $.proxy( function() {
				this.remove( this.$notice );
			}, this ), this.option.delay ) ).find( '.close' ).on( 'click', $.proxy( function() {
				this.off( 'mouseleave' );
				BigweNotice.prototype.remove( this );
			}, this.$notice ) );

			// force reflow
			// 此处可以使动画能够顺利完成
			this.$notice[ 0 ].offsetWidth;
			this.$notice.css( this.getStyle( this.$notice ) ).addClass( 'in' );
		}
		return this;
	}
	/** 移除通知消息 */
	BigweNotice.prototype.remove = function( $element ) {
		$element.one( $.support.transition.end, function() {
			$element.remove();
		} ).css( {
			'-webkit-transform': '',
			'-ms-transform': '',
			'-o-transform': '',
			'transform': ''
		} ).removeClass( 'in' );
	}
	/** 获取translateTop */
	BigweNotice.prototype.getStyle = function() {
		var windowWidth = this.$window.width();
		var headerWidth = 1200;
		var right = windowWidth / 2 - headerWidth / 2;
		var translateTop = 4;
		var topNavHeight = $( '.top_main_nav', this.$body ).outerHeight();
		var scrollTop = $( document ).scrollTop();
		if ( scrollTop < topNavHeight ) {
			translateTop += topNavHeight - scrollTop;
		}
		$( '.system_notice', this.$body ).each( function( index ) {
			if ( index ) {
				translateTop += $( this ).outerHeight( true );
			}
		} );
		return {
			'-webkit-transform': 'translateY(' + translateTop + 'px)',
			'-ms-transform': 'translateY(' + translateTop + 'px)',
			'-o-transform': 'translateY(' + translateTop + 'px)',
			'transform': 'translateY(' + translateTop + 'px)'
		};
	}

	return BigweNotice;
} );